import logging

from flask import Blueprint, request
from flask import jsonify

from airflow.patsnap.service import role_service
from airflow.exceptions import AirflowException
from airflow.models.idata_auth_role import AuthRole
from airflow.utils.session import create_session

log = logging.getLogger(__name__)

api_speech_role = Blueprint('api_speech_role', __name__)


@api_speech_role.route('/roles/<int:rid>', methods=['GET'])
def get_role(rid):
    try:
        with create_session() as session:
            role = AuthRole.get_role(rid, session)
            return jsonify(code=0, message='ok', data=AuthRole.to_json(role))
    except AirflowException as e:
        log.error("get_role error =%s, params=%s", e, rid, )
        return jsonify(code=500, message=str(e), data=None)


@api_speech_role.route('/roles', methods=['GET'])
def get_roles():
    try:
        page = request.args.get('page', 0, type=int)
        result = role_service.get_roles(page)
        return jsonify(code=0, message='ok', data=result)
    except AirflowException as e:
        log.error("get_roles error =%s", e, )
        return jsonify(code=500, message=str(e), data=None)


@api_speech_role.route('/roles', methods=['POST'])
def add_role():
    try:
        role_name = request.values.get("role_name", type=str, default=None)
        weight = request.values.get("weight", type=str, default=None)
        resource_ids = str(request.json)
        if role_name is None:
            log.error("add_role error role_name=%s", role_name, )
            return jsonify(code=400, message="add_role error name=None", data=None)
        if weight is None:
            log.error("add_role error weight=%s", weight, )
            return jsonify(code=400, message="add_role error url_prefix=None", data=None)

        role_service.add_role(role_name, weight, resource_ids)
        return jsonify(code=0, message='ok', data=None)
    except AirflowException as e:
        log.error("add_role error =%s,", e, )
        return jsonify(code=500, message=str(e), data=None)


@api_speech_role.route('/roles/<int:id>', methods=['DELETE'])
def delete_role(id):
    try:
        role_service.delete_role(id)
        return jsonify(code=0, message='ok', data=None)
    except AirflowException as e:
        log.error("delete_role error =%s, params=%s", e, id, )
        return jsonify(code=500, message=str(e), data=None)


@api_speech_role.route('/roles', methods=['PUT'])
def update_role():
    try:
        id = request.values.get("id", type=int, default=None)
        name = request.values.get("role_name", type=str, default=None)
        weight = request.values.get("weight", type=str, default=None)
        resource_ids = str(request.json)
        if id is None:
            log.error("update_role error id=%s", id, )
            return jsonify(code=400, message="add_role error id=None", data=None)
        if name is None:
            log.error("update_role error role_name=%s", name, )
            return jsonify(code=400, message="add_role error role_name=None", data=None)
        if weight is None:
            log.error("update_role error weight=%s", weight, )
            return jsonify(code=400, message="add_role error weight=None", data=None)

        role_service.update_role(id, name, weight, resource_ids)
        return jsonify(code=0, message='ok', data=None)
    except AirflowException as e:
        log.error("update_role error =%s,", e, )
        return jsonify(code=500, message=str(e), data=None)
